<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.code" placeholder="编号" clearable></el-input>
                    <el-input v-model="search.name" placeholder="机构名称" clearable></el-input>
                    <el-input v-model="search.chargeName" placeholder="负责人" clearable></el-input>
                    <el-input v-model="search.chargeMobile" placeholder="负责人电话" clearable></el-input>
                    <el-select
                        v-model="search.parentId"
                        filterable
                        remote
                        clearable
                        placeholder="归属机构"
                        :remote-method="searchAgentHander"
                        :loadings="agentLoading">
                        <el-option
                            v-for="item in agentList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            :disabled="item.disabled">
                        </el-option>
                    </el-select>
                    <el-select v-model="search.levelId" placeholder="机构等级" clearable>
                        <el-option :label="item.name" :value="item.id" v-for="item in agentLevels"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="状态" clearable>
                        <el-option label="禁用" :value="0"></el-option>
                        <el-option label="启用" :value="1"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.agent.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="编号" prop="code" width="120" align="center"></el-table-column>
                    <el-table-column label="名称" prop="name" align="center" width="200"></el-table-column>
                    <el-table-column label="登录账号" prop="account" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.authAccount ? scope.row.authAccount.account : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="归属机构" prop="parent" align="center" width="180">
                        <template #default="scope">
                            {{ scope.row.parent ? scope.row.parent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="机构等级" prop="level" align="center" width="160">
                        <template #default="scope">
                            {{ scope.row.level ? scope.row.level.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="负责人" prop="chargeName" width="130" align="center"></el-table-column>
                    <el-table-column label="负责人电话" prop="chargeMobile" width="150" align="center"></el-table-column>
                    <el-table-column label="30课时" prop="classHour30" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="60课时" prop="classHour60" width="120" align="center">
                    </el-table-column>
                    <!-- <el-table-column label="体验课" prop="trialCourse" width="120" align="center">
                    </el-table-column> -->
                    <el-table-column label="累计30课时" prop="classHour30" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.classHourStatistics ? scope.row.classHourStatistics.classHour30Total : 0 }}
                        </template>
                    </el-table-column>
                    <el-table-column label="累计60课时" prop="classHour60" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.classHourStatistics ? scope.row.classHourStatistics.classHour60Total : 0 }}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="累计体验课" prop="trialCourseTotal" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.classHourStatistics ? scope.row.classHourStatistics.trialCourseTotal : 0 }}
                        </template>
                    </el-table-column> -->
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="danger">{{scope.row.status.name}}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="200">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row)">查看</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_edit(scope.row)">编辑</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-dropdown>
                                <el-button type="text" size="small">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-dropdown-item @click="table_recharge(scope.row)">充值</el-dropdown-item>
                                        <el-dropdown-item divided  @click="table_refund(scope.row)">退费</el-dropdown-item>
                                        <el-popconfirm v-if="scope.row.status.value == 0" :title="'确认是否要启用机构: '+scope.row.name+'?'" @confirm="table_status(scope.row, 1)">
                                            <template #reference>
                                                <el-dropdown-item divided>启用</el-dropdown-item>
                                            </template>
                                        </el-popconfirm>
                                        <el-popconfirm v-if="scope.row.status.value == 1" :title="'确认是否要禁用机构: '+scope.row.name+'?'" @confirm="table_status(scope.row, 0)">
                                            <template #reference>
                                                <el-dropdown-item divided>禁用</el-dropdown-item>
                                            </template>
                                        </el-popconfirm>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" :agentLevels="agentLevels" :agentCategorys="agentCategorys" :areaData="areaData" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <recharge-dialog v-if="dialog.recharge" ref="rechargeDialog" @closed="dialog.recharge=false" @success="handleSuccess"></recharge-dialog>

    <refund-dialog v-if="dialog.refund" ref="refundDialog" @closed="dialog.refund=false" @success="handleSuccess"></refund-dialog>

</template>

<script>
import saveDialog from './save'
import detailDialog from './detail'
import rechargeDialog from './components/recharge'
import refundDialog from './components/refund'
export default {
    name: 'user',
    components: {
        saveDialog,
        detailDialog,
        rechargeDialog,
        refundDialog
    },
    data() {
        return {
            agentLoading: false,
            dialog: {
                save: false,
                detail: false,
                recharge: false,
                refund: false,
            },
            selection: [],
            search: {
                name: null,
            },
            agentLevels: [],
            agentCategorys: [],
            areaData: [],
            agentList: [],
        }
    },
    mounted() {
        this.getAllAgentCategory()
        this.getAllAgentLevel()
        this.getArea()
        this.searchAgentHander()
    },
    methods: {
        getAllAgentLevel() {
            this.$API.agent.level.all.get({}).then(res => {
                this.agentLevels = res.data
            })
        },
        getAllAgentCategory() {
            this.$API.agent.category.all.get({}).then(res => {
                this.agentCategorys = res.data
            })
        },
        getArea() {
            this.$API.area.tree.get({}).then(res => {
                this.areaData = res.data
            })
        },
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        // 增加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        // 编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id, 'first')
			})
        },
        //状态变更
        async table_status(row, status) {
            await this.$API.agent.updateStatus.put({ id: row.id, status: status }).then(() => {
                this.$notify.success("更新成功")
                this.$refs.table.refresh()
            })
        },
        // 充值
        table_recharge(row) {
            this.dialog.recharge = true
            this.$nextTick(() => {
                this.$refs.rechargeDialog.open('机构【'+row.name+'】课时充值').setData(row)
            })
        },
        // 退费
        table_refund(row) {
            this.dialog.refund = true
            this.$nextTick(() => {
                this.$refs.refundDialog.open('机构【'+row.name+'】课时退费').setData(row)
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        },
        searchAgentHander(query) {
            this.agentLoading = true;
            this.$API.agent.listByName.get({name:query}).then((res) => {
                this.agentList = res.data
            }).finally(() => {
                this.agentLoading = false
            });
        }
    }
}
</script>

<style>
</style>
